package de.smarthome.controllable;

import de.smarthome.database.DatabaseHandler;
import de.smarthome.message.Connector;

public class MasterController {

	private Connector connector = new Connector();
	private DatabaseHandler dbHandler;
	private final static String FLOOR = "floor";
	private final static String DININGROOM = "diningroom";
	private final static String BEDROOM = "bedroom";
	private final static String LIVINGROOM = "livingroom";
	private final static String KITCHEN = "kitchen";

	public MasterController(DatabaseHandler db) {
		dbHandler = db;
	}

	public void lightOn(String room) {
		if (room.equals(FLOOR)) {
			connector.lightControl(LPLight.corridorLightOn());
		}
		if (room.equals(BEDROOM)) {
			connector.lightControl(LPLight.sleepingLightOn());
		}
		if (room.equals(LIVINGROOM)) {
			connector.lightControl(LPLight.loungeLightOn());
		}
		if (room.equals(KITCHEN)) {
			connector.lightControl(LPLight.kitchenCookingLightOn());
			connector.lightControl(LPLight.kitchenMainLightOn());
		}
		if (room.equals(DININGROOM)) {
			connector.lightControl(LPLight.diningLightOn());
		}

		dbHandler.updateRoomStats(room, "light", 1);
	}

	public void lightOff(String room) {
		if (room.equals(FLOOR)) {
			connector.lightControl(LPLight.corridorLightOff());
		}
		if (room.equals(BEDROOM)) {
			connector.lightControl(LPLight.sleepingLightOff());
		}
		if (room.equals(LIVINGROOM)) {
			connector.lightControl(LPLight.loungeLightOff());
		}
		if (room.equals(KITCHEN)) {
			connector.lightControl(LPLight.kitchenCookingLightOff());
			connector.lightControl(LPLight.kitchenMainLightOff());
		}
		if (room.equals(DININGROOM)) {
			connector.lightControl(LPLight.diningLightOff());
		}
		dbHandler.updateRoomStats(room, "light", 0);
	}

	public void changeColor(String room, int color) {
		if (room.equals(FLOOR)) {
			connector.lightColorControl(LPLight.corridorLightColor(color));
		}
		if (room.equals(BEDROOM)) {
			connector.lightColorControl(LPLight.sleepingLightColor(color));
		}
		if (room.equals(LIVINGROOM)) {
			connector.lightColorControl(LPLight.loungeLightColor(color));
		}
		if (room.equals(KITCHEN)) {
			connector.lightColorControl(LPLight.kitchenCookingLightColor(color));
			connector.lightColorControl(LPLight.kitchenMainLightColor(color));
		}
		if (room.equals(DININGROOM)) {
			connector.lightColorControl(LPLight.diningLightColor(color));
		}
		dbHandler.updateRoomStats(room, "color", color);
	}

	public void noColor(String room) {
		if (room.equals(FLOOR)) {
			connector.lightColorControl(LPLight.corridorLightColor(0));
		}
		if (room.equals(BEDROOM)) {
			connector.lightColorControl(LPLight.sleepingLightColor(0));
		}
		if (room.equals(LIVINGROOM)) {
			connector.lightColorControl(LPLight.loungeLightColor(0));
		}
		if (room.equals(KITCHEN)) {
			connector.lightColorControl(LPLight.kitchenCookingLightColor(0));
			connector.lightColorControl(LPLight.kitchenMainLightColor(0));
		}
		if (room.equals(DININGROOM)) {
			connector.lightColorControl(LPLight.diningLightColor(0));
		}
		dbHandler.updateRoomStats(room, "color", 0);

	}

	public void curtainOpen(String room) {
		if (room.equals(FLOOR)) {
			connector.curtainControl(LPCurtain.sleepingHallCurtainOpen());
		}
		if (room.equals(BEDROOM)) {
			connector.curtainControl(LPCurtain.sleepingWindowCurtainOpen());
		}
		if (room.equals(LIVINGROOM)) {
			connector.curtainControl(LPCurtain.loungeCurtainOpen());
		}

		dbHandler.updateRoomStats(room, "curtain", 1);
	}

	public void curtainClose(String room) {
		if (room.equals(FLOOR)) {
			connector.curtainControl(LPCurtain.sleepingHallCurtainClose());
		}
		if (room.equals(BEDROOM)) {
			connector.curtainControl(LPCurtain.sleepingWindowCurtainClose());
		}
		if (room.equals(LIVINGROOM)) {
			connector.curtainControl(LPCurtain.loungeCurtainClose());
		}
		dbHandler.updateRoomStats(room, "curtain", 0);
	}

	public void windowOpen(String room) {
		if (room.equals(DININGROOM)) {
			connector.windowControl(LPWindow.winDiningOneOpen());
			connector.windowControl(LPWindow.winDiningZeroOpen());

		}
		if (room.equals(KITCHEN)) {
			connector.windowControl(LPWindow.winKitchenZeroOpen());
			connector.windowControl(LPWindow.winKitchenOneOpen());
		}

		if (room.equals(LIVINGROOM)) {
			connector.windowControl(LPWindow.winLoungeZeroOpen());
			connector.windowControl(LPWindow.winLoungeOneOpen());
			connector.windowControl(LPWindow.winLoungeTwoOpen());
			connector.windowControl(LPWindow.winLoungeThreeOpen());
			connector.windowControl(LPWindow.winLoungeFourOpen());
			}
		dbHandler.updateRoomStats(room, "window", 1);
	}

	public void windowClose(String room) {
		if (room.equals(DININGROOM)) {
			connector.windowControl(LPWindow.winDiningOneClose());
			connector.windowControl(LPWindow.winDiningZeroClose());

		}
		if (room.equals(KITCHEN)) {
			connector.windowControl(LPWindow.winKitchenZeroClose());
			connector.windowControl(LPWindow.winKitchenOneClose());
		}

		if (room.equals(LIVINGROOM)) {
			connector.windowControl(LPWindow.winLoungeZeroClose());
			connector.windowControl(LPWindow.winLoungeOneClose());
			connector.windowControl(LPWindow.winLoungeTwoClose());
			connector.windowControl(LPWindow.winLoungeThreeClose());
			connector.windowControl(LPWindow.winLoungeFourClose());
			}
		dbHandler.updateRoomStats(room, "window", 0);
	}

	public void blindOpen(String room) {
		if (room.equals(DININGROOM)) {
			connector.blindControl(LPBlind.blindDiningRoomOpen());
		}

		if (room.equals(KITCHEN)) {
			connector.blindControl(LPBlind.blindDiningRoomOpen());
		}

		if (room.equals(LIVINGROOM)) {
			connector.blindControl(LPBlind.blindLoungeOpen());
		}

		if (room.equals(BEDROOM)) {
			connector.blindControl(LPBlind.blindSleepingOpen());
		}
		dbHandler.updateRoomStats(room, "blind", 1);
	}

	public void blindClose(String room) {
		if (room.equals(DININGROOM)) {
			connector.blindControl(LPBlind.blindDiningRoomClose());
		}

		if (room.equals(KITCHEN)) {
			connector.blindControl(LPBlind.blindDiningRoomClose());
		}

		if (room.equals(LIVINGROOM)) {
			connector.blindControl(LPBlind.blindLoungeClose());
		}

		if (room.equals(BEDROOM)) {
			connector.blindControl(LPBlind.blindSleepingClose());
		}
		dbHandler.updateRoomStats(room, "blind", 0);
	}
}
